package com.showcai.controller.admin;

import com.showcai.controller.base.BaseController;
import com.showcai.module.AdminMenu;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.result.ResultObj;
import com.showcai.result.ReturnCode;
import com.showcai.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2018-9-6.
 */
@Controller
@RequestMapping(value = "/menu")
public class MenuController extends BaseController {
@Autowired
    MenuService menuService;

    @RequestMapping(value = "/getMenuList", method = RequestMethod.POST)
    @ResponseBody
    public String getMenuList() {

        ResultObj resultObj = new ResultObj();

        try {
            Map<String, String> map = this.showRequestParameters();

            List<AdminMenu> menuList = menuService.getMenuList(map);
            resultObj.setData(menuList);
            resultObj.setMsg("成功！");
        } catch (MyRunTimeExcption myRunTimeExcption) {

            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }

        return resultObj.toJson();

    }

    @RequestMapping(value = "/showMenuList")
    public ModelAndView showMenuList() {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/menu/getMenuList");

        Map<String, String> map = this.showRequestParameters();
        map.put("menuParentId", "0");
        List<AdminMenu> menuList = menuService.getMenuList(map);

        mov.addObject("menuList", menuList);

        mov.addObject("bean", map);
        return mov;

    }

    /**
     * 添加菜单
     * @param adminMenu
     * @return
     */
    @RequestMapping(value="/addMenu", method = RequestMethod.POST)
    @ResponseBody
    public String addMenu(AdminMenu adminMenu) {
        System.out.println("=================================addMenu==========================================>>>>>>");
        ResultObj resultObj = new ResultObj();
        try {
           /* Map<String, String> map = this.showRequestParameters();*/
            resultObj.setMsg("成功！");
            menuService.addMenu(adminMenu);
        } catch (MyRunTimeExcption myRunTimeExcption) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  resultObj.toJson();
    }
}
